
//
//  RegisteViewController.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2017/12/23.
//  Copyright © 2017年 卢仕彤. All rights reserved.
//

#import "RegisteViewController.h"
#import "LoginTextField.h"
#import "UIColor+Expand.h"
#import "FFNetworkHelper.h"
#import "NSString+Common.h"

@interface RegisteViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIView *whiteBgView;
@property (nonatomic, strong) UILabel *registLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *passwordLabel;
@property (nonatomic, strong) UILabel *verificationLabel;
@property (nonatomic, strong) LoginTextField *phoneTextField;
@property (nonatomic, strong) LoginTextField *passwordTextField;
@property (nonatomic, strong) LoginTextField *verificationTextField;
@property (nonatomic, strong) UIButton *sendMessgaeButton;
@property (nonatomic, strong) UIButton *registButton;
@end

@implementation RegisteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configRegister];
}

#pragma -mark ------------------lazy init----------------------

- (void)configRegister {
    [self.view addSubview:self.whiteBgView];
    [self.whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.whiteBgView addSubview:self.registLabel];
    [self.registLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.whiteBgView.centerX);
        make.top.equalTo(self.whiteBgView.top).offset(40);
    }];
    
    [self.whiteBgView addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView.left).offset(30);
        make.top.equalTo(self.registLabel.bottom).offset(80);
    }];
    
    [self.whiteBgView addSubview:self.phoneTextField];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneLabel.mas_left);
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.top.equalTo(self.phoneLabel.mas_bottom).offset(10);
    }];
    
    [self.whiteBgView addSubview:self.passwordLabel];
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView.mas_left).offset(30);
        make.centerX.equalTo(self.whiteBgView.centerX);
        make.top.equalTo(self.phoneLabel.mas_bottom).offset(80);
    }];
    
    [self.whiteBgView addSubview:self.passwordTextField];
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLabel.mas_left);
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.top.equalTo(self.passwordLabel.mas_bottom).offset(10);
    }];
    
    [self.whiteBgView addSubview:self.verificationLabel];
    [self.verificationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLabel.left);
        make.top.equalTo(self.passwordTextField.bottom).offset(40);
    }];
    
    [self.whiteBgView addSubview:self.verificationTextField];
    [self.verificationTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLabel.mas_left);
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.top.equalTo(self.verificationLabel.mas_bottom).offset(10);
    }];
    
    [self.whiteBgView addSubview:self.sendMessgaeButton];
    [self.sendMessgaeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.bottom.equalTo(self.verificationLabel.bottom);
    }];
    
    [self.whiteBgView addSubview:self.registButton];
    [self.registButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView.mas_left).offset(30);
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.centerX.equalTo(self.whiteBgView.mas_centerX);
        make.bottom.equalTo(self.whiteBgView.mas_bottom).offset(-60);
    }];
}

- (UIView *) whiteBgView {
    if (!_whiteBgView) {
        _whiteBgView = [[UIView alloc] init];
        _whiteBgView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteBgView;
}

- (UILabel *)registLabel {
    if (!_registLabel) {
        _registLabel = [UILabel labelWithSystemFontSize:18 textColorHexString:@"#333333"];
        _registLabel.font = [UIFont boldSystemFontOfSize:18];
        _registLabel.text = @"注册";
    }
    return _registLabel;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [UILabel labelWithSystemFontSize:15 textColorHexString:@"#333333"];
        _phoneLabel.text = @"手机号";
    }
    return _phoneLabel;
}

- (UILabel *)passwordLabel {
    if (!_passwordLabel) {
        _passwordLabel = [UILabel labelWithSystemFontSize:15 textColorHexString:@"#333333"];
        _passwordLabel.text = @"密码";
    }
    return _passwordLabel;
}

- (UILabel *)verificationLabel {
    if (!_verificationLabel) {
        _verificationLabel = [UILabel labelWithSystemFontSize:15 textColorHexString:@"#333333"];
        _verificationLabel.text = @"验证码";
    }
    return _verificationLabel;
}

- (LoginTextField *)passwordTextField {
    if (!_passwordTextField) {
        _passwordTextField = [[LoginTextField alloc] init];
        _passwordTextField.delegate = self;
        _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
        _passwordTextField.placeholder = @"请输入密码";
        _passwordTextField.font = [UIFont systemFontOfSize:16];
        _passwordTextField.textColor = [UIColor colorWithHexString:@"333333"];;
        _passwordTextField.returnKeyType = UIReturnKeyDone;
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.rightViewMode = UITextFieldViewModeAlways;
        [_passwordTextField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        [_passwordTextField setValue:[UIColor colorWithHexString:@"D8D8D8"] forKeyPath:@"_placeholderLabel.textColor"];
        _passwordTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _passwordTextField;
}

- (LoginTextField *) phoneTextField {
    
    if (!_phoneTextField) {
        _phoneTextField = [[LoginTextField alloc] init];
        _phoneTextField.delegate = self;
        _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
        _phoneTextField.placeholder = @"请输入手机号";
        _phoneTextField.font = [UIFont systemFontOfSize:16];
        _phoneTextField.textColor = [UIColor colorWithHexString:@"333333"];;
        _phoneTextField.returnKeyType = UIReturnKeyDone;
        _phoneTextField.rightViewMode = UITextFieldViewModeAlways;
        [_phoneTextField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        [_phoneTextField setValue:[UIColor colorWithHexString:@"D8D8D8"] forKeyPath:@"_placeholderLabel.textColor"];
        _phoneTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _phoneTextField;
}

- (LoginTextField *)verificationTextField {
    if (!_verificationTextField) {
        _verificationTextField = [[LoginTextField alloc] init];
        _verificationTextField.delegate = self;
        _verificationTextField.leftViewMode = UITextFieldViewModeAlways;
        _verificationTextField.placeholder = @"请输入验证码";
        _verificationTextField.font = [UIFont systemFontOfSize:16];
        _verificationTextField.textColor = [UIColor colorWithHexString:@"333333"];;
        _verificationTextField.returnKeyType = UIReturnKeyDone;
        _verificationTextField.rightViewMode = UITextFieldViewModeAlways;
        [_verificationTextField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        [_verificationTextField setValue:[UIColor colorWithHexString:@"D8D8D8"] forKeyPath:@"_placeholderLabel.textColor"];
        _verificationTextField.keyboardType = UIKeyboardTypeNumberPad;
        
    }
    return _verificationTextField;
}

- (UIButton *)sendMessgaeButton {
    if (!_sendMessgaeButton) {
        _sendMessgaeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sendMessgaeButton setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
        [_sendMessgaeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        _sendMessgaeButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_sendMessgaeButton addTarget:self action:@selector(sendMessageInfo) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendMessgaeButton;
}

- (UIButton *)registButton {
    if (!_registButton) {
        _registButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_registButton addTarget:self action:@selector(registAction) forControlEvents:UIControlEventTouchUpInside];
        [_registButton setTitle:@"注册" forState:UIControlStateNormal];
        _registButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _registButton.layer.cornerRadius = 18;
        _registButton.layer.borderWidth = 0.5;
        _registButton.layer.borderColor = [UIColor colorWithHexString:@"414C50"].CGColor;
        _registButton.layer.masksToBounds = YES;
    }
    return _registButton;
    
}

- (void)sendMessageInfo {
    
    if (![_phoneTextField.text isPhoneNo]) {
        return;
    }
    
    NSString *requestUrl = @"user/sendCaptchaForRegister";
    NSDictionary *parmsDic = @{@"phone":_phoneTextField.text};
    [FFNetworkHelper closeSig];
    [FFNetworkHelper POST:requestUrl parameters:parmsDic success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 300) {
            
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void) registAction {
    NSString *requestUrl = @"user/register";
    NSDictionary *parmsDic = @{@"phone":_phoneTextField.text,
                               @"password":_passwordTextField.text,
                               @"code":_verificationTextField.text
                               };
    [FFNetworkHelper openLog];
    [FFNetworkHelper closeSig];
    [FFNetworkHelper POST:requestUrl parameters:parmsDic success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
